/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **      It is a Atomic action to check current site role 
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

/**
 * It is a Atomic action to check current site role.
 * The action need to configure a "Role" parameter as checking condition, the Role should be configured to "Master" or "Slave" value.
 * If the current site role matched the setting role, the action completed successfully. 
 * If not matched, a BusinessException will be thrown, and interrupt the execution.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua
 * Date: Apr 10, 2015
 * 
 * </P>
 *
 **/
import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.exception.BusinessException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class CheckCurrentSiteRoleActionHandler extends AbstractActionHandler {

    private final static String MASTER = "Master";
    private final static String SLAVE = "Slave";
    private static Logger logger = LoggerFactory.getLogger(CheckCurrentSiteRoleActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {


        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);
        String currentSiteName = Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME);
        String role = parameterMap.get("Role");
        if (MASTER.equalsIgnoreCase(role)) {
            boolean isMasterSite = Utils.checkMasterSite(currentSiteName);
            if (logger.isDebugEnabled()) {
                logger.debug("Current site is master site:{}", isMasterSite);
            }
            if (!isMasterSite)
                throw new BusinessException("Current site is not a master site!!");
        } else if (SLAVE.equalsIgnoreCase(role)) {
            boolean isSlaveSite = Utils.checkSlaveSite(currentSiteName);
            if (logger.isDebugEnabled()) {
                logger.debug("Current site is slave site:{}", isSlaveSite);
            }
            if (!isSlaveSite)
                throw new BusinessException("Current site is not a slave site!!");
        } else {
            throw new ActionException("the role value is wrong{" + role + "}, its value shall be 'Master' or 'Slave', pelase you check configuration files");
        }

    }

}
